package com.google.gwt.sample.json.client;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.sample.json.client.SearchResult.JSONCountResponseTextHandler;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.ui.*;

public class Header extends Composite {

		static public class JSONResponseTextHandler implements ResponseTextHandler {
			private JSONValue jsonValue;
			private SearchResult searchResult;
			private String searchText;
			private boolean clearMainPage;
			public JSONResponseTextHandler(SearchResult searchResult, String searchText, boolean clearMainPage){
				this.searchResult = searchResult;
				this.searchText = searchText;
				this.clearMainPage = clearMainPage;
			}
			public void onCompletion(String responseText) {
				try {
					jsonValue = JSONParser.parse(responseText);
					if (jsonValue != null){
						JSONArray jsonArray = jsonValue.isArray();
						if (jsonArray != null) {
							searchResult.setLabels(jsonArray, searchText);
							MainPage.setMainPanel(this.searchResult, clearMainPage);
						}					
					}
				} catch (JSONException e) {
				}
			}
		}

	private TextBox tb = new TextBox();

	public Header() {		
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final Label playfulInventionDatabaseLabel = new Label("Playful Invention Database");
		verticalPanel.add(playfulInventionDatabaseLabel);
		playfulInventionDatabaseLabel.setSize("700px", "50px");
		playfulInventionDatabaseLabel.setStyleName("gwt-Header");

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setStyleName("gwt-SearchLabel");
		verticalPanel.setCellHorizontalAlignment(horizontalPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel.setWidth("700px");
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel.add(horizontalPanel_1);
		horizontalPanel.setCellHorizontalAlignment(horizontalPanel_1, HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel_1.setWidth("500px");
		horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		tb.setName("content");
		horizontalPanel_1.add(tb);
		tb.setSize("100%", "25px");
		horizontalPanel_1.setCellHorizontalAlignment(tb, HasHorizontalAlignment.ALIGN_RIGHT);
	    tb.addKeyboardListener(new KeyboardListenerAdapter() {
	        public void onKeyPress(Widget sender, char keyCode, int modifiers) {
	          if (keyCode == (char) KEY_ENTER){
	        	  doSearch();
	          }
	        }
	      });


		final Button button = new Button("searchy-poo-bee", new ClickListener() {
			public void onClick(Widget sender) {
	        	  doSearch();
			}
		});
		horizontalPanel_1.add(button);
		button.setHeight("25px");
		
		final HeaderTab headerTab = new HeaderTab();
		headerTab.setSearchBox(tb);
		verticalPanel.add(headerTab);

	}
	private void doSearch(){
			String searchText = "/python/workshops/count?search=" + tb.getText();
			HTTPRequest.asyncGet(searchText, new JSONCountResponseTextHandler());		        	  
			searchText = "/python/workshops?search=" + tb.getText();
			HTTPRequest.asyncGet(searchText, new JSONResponseTextHandler(new WorkshopSearchResult(false), tb.getText(), true));
			searchText = "/python/codesamples/count?search=" + tb.getText();
			HTTPRequest.asyncGet(searchText, new JSONCountResponseTextHandler());		        	  
  			searchText = "/python/codesamples?search=" + tb.getText();
			HTTPRequest.asyncGet(searchText, new JSONResponseTextHandler(new CodeSearchResult(false), tb.getText(), false));	
	}
}
